package com.sky.service.impl;

import com.sky.exception.BaseException;
import com.sky.service.ShopStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class ShopStatusServiceImpl implements ShopStatusService {
    //注入redisTemplate模版对象用来操作redis缓存数据
    @Autowired
    private RedisTemplate redisTemplate;
    //创建一个常量,用来存放redis缓存的key值
    public static final String SHOP_STATUS_KEY = "shop_status";
    @Override
    public Integer getShopStatus() {
        //创建用于redis数据库的操作对象
        ValueOperations stringValueOperations = redisTemplate.opsForValue();
        //业务实现,获取店铺营业状态
        String shopStatus = (String) stringValueOperations.get(SHOP_STATUS_KEY);
        //转换为Integer类型
        if (shopStatus == null) {
            //抛出一个"店铺营业状态未设置"的异常
            throw new BaseException("店铺营业状态未设置");
        }

        return Integer.parseInt(shopStatus);
    }

    @Override
    public void sethopStatus(Integer status) {
        //创建用于redis数据库的操作对象
        ValueOperations stringValueOperations = redisTemplate.opsForValue();
        //业务实现,设置店铺营业状态
        stringValueOperations.set(SHOP_STATUS_KEY, String.valueOf(status));

    }
}
